#!/usr/bin/perl

#===============================================================================
#     REVISION:  $Id$
#  DESCRIPTION:  Test for Meter::Appartments
#===============================================================================

use strict;
use warnings;

use Readonly;
Readonly our $VERSION => qw($Revision$) [1];

use English qw( -no_match_vars );

#use Smart::Comments;
#$ENV{'DONT_CLEANUP'} = 1;

use Meter::Test qw(
    init_tmp_db
);

use Meter::Users;
use Meter::Houses;
use Meter::Appartments;
use Meter::Utils qw(
    dump_compact
);

use Test::More tests => 8;
use Test::Exception;
use Test::DatabaseRow;

Readonly my $USERNAME => 'user1';
Readonly my $EMAIL    => 'email1@example.com';
Readonly my $PASSWORD => '123456';

Readonly my $ADDRESS => 'Коломяжский пр-т 15-1';

Readonly my $APPARTMENT_NUMBER => 123;

Readonly my $FIRST_NAME              => 'Александр';
Readonly my $LAST_NAME               => 'Симаков';
Readonly my $MIDDLE_NAME             => 'Владимирович';
Readonly my $COLD_WATER_METER_NUMBER => '111111';
Readonly my $HOT_WATER_METER_NUMBER  => '222222';

sub _create_user {
    my $user = Meter::Users->insert(
        {   username => $USERNAME,
            password => $PASSWORD,
            email    => $EMAIL,
        }
    );

    return $user;
}

sub _create_house {
    my $house = Meter::Houses->insert( { address => $ADDRESS } );

    return $house;
}

sub test_common_case {
    my $appartment_fields = {
        appartment_number       => $APPARTMENT_NUMBER,
        first_name              => $FIRST_NAME,
        last_name               => $LAST_NAME,
        middle_name             => $MIDDLE_NAME,
        cold_water_meter_number => $COLD_WATER_METER_NUMBER,
        hot_water_meter_number  => $HOT_WATER_METER_NUMBER,
    };

    # create appartment
    my $user = _create_user();
    lives_ok {
        Meter::Appartments->insert(
            {   user  => $user,
                house => _create_house(),
                %{$appartment_fields},
            }
        );
    }
    'Create new appartment '
        . dump_compact( $appartment_fields, 'APPARTMENT' );

    row_ok(
        table => 'appartments',
        where => [ %{$appartment_fields} ],
        label => 'Check database row for '
            . dump_compact( $appartment_fields, 'APPARTMENT' ),
    );

    # retrieve appartment
    my $appartment = Meter::Appartments->retrieve( %{$appartment_fields} );
    ok( $appartment,
        'Retrieve appartment '
            . dump_compact( $appartment_fields, 'APPARTMENT' ) );

    is( $user->appartment(), $appartment, 'Get appartment by user' );

    # update appartment
    Readonly my $NEW_APPARTMENT_NUMBER => 456;
    my $updated_appartment_fields = {
        %{$appartment_fields}, appartment_number => $NEW_APPARTMENT_NUMBER,
    };

    lives_ok {
        $appartment->appartment_number($NEW_APPARTMENT_NUMBER);
        $appartment->update();
    }
    'Update appartment: '
        . dump_compact( $updated_appartment_fields, 'APPARTMENT' );

    row_ok(
        table => 'appartments',
        where => [ %{$updated_appartment_fields} ],
        label => 'Check database row for updated '
            . dump_compact( $updated_appartment_fields, 'APPARTMENT' ),
    );

    # delete appartment
    lives_ok {
        $appartment->delete();
    }
    'Delete appartment';

    not_row_ok(
        table => 'appartments',
        where => [ appartment_number => $NEW_APPARTMENT_NUMBER ],
        label =>
            "Check NO database row for deleted appartment '$NEW_APPARTMENT_NUMBER'",
    );

    return;
}

sub run_tests {
    init_tmp_db();

    test_common_case();

    return;
}

run_tests();
